package com.lzx.iteam.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {


	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String formatDate(Date date, String format) {
		SimpleDateFormat outFormat = new SimpleDateFormat(format);
		return outFormat.format(date);
	}

	/**
	 * Formats a Date object to return a date using the global locale.
	 */
	public static String formatDate(Date date) {
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
		return outFormat.format(date);
	}

	/**
	 * Formats a Date object to return a date and time using the global locale.
	 */
	public static String formatDateTime(Date date) {
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return outFormat.format(date);
	}

	public static String formatDate2(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatDate3(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatDate4(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatMonthDay(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatDate5(Date myDate) {
		String strDate = getYear(myDate) + "-" + getMonth(myDate) + "-"
				+ getDay(myDate);
		return strDate;
	}

	public static String formatDate6(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static long Date2Long(int year, int month, int date) {
		Calendar cld = Calendar.getInstance();
		month = month - 1;
		cld.set(year, month, date);
		return cld.getTime().getTime();
	}

	public static long Time2Long(int year, int month, int date, int hour,
								 int minute, int second) {
		Calendar cld = Calendar.getInstance();
		month = month - 1;
		cld.set(year, month, date, hour, minute, second);
		return cld.getTime().getTime();
	}

	public static int getYear(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new Date(t));
		}
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new Date(t));
		}
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new Date(t));
		}
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getWeek() {
		int week=0;
		Calendar cld = Calendar.getInstance();
		switch (cld.get(Calendar.DAY_OF_WEEK)) {
			case 1:
				week= 7;
				break;
			case 2:
				week=1;
				break;
			case 3:
				week= 2;
				break;
			case 4:
				week= 3;
				break;
			case 5:
				week= 4;
				break;
			case 6:
				week= 5;
				break;
			case 7:
				week= 6;
				break;

		}
		return week;

	}

	public static int getHour(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new Date(t));
		}
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new Date(t));
		}
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new Date(t));
		}
		return cld.get(Calendar.SECOND);
	}

	public static String getCompleteDate(long t) {
		StringBuilder dateStr = new StringBuilder("");
		dateStr.append(DateUtil.getYear(t) + "年");
		dateStr.append(DateUtil.getMonth(t) + "月");
		dateStr.append(DateUtil.getDay(t) + "日");
		dateStr.append(DateUtil.getHour(t) + "时");
		dateStr.append(DateUtil.getMinute(t) + "分");
		return dateStr.toString();
	}

	/**
	 * �ж�ʱ���Ƿ���ͬһ��
	 * @param current
	 * @param old
	 * @return
	 */
	public static boolean isTheSameDay(long current,long old) {
		boolean result = false;

		result = DateUtil.getYear(current) == DateUtil.getYear(old);

		if(result) {
			result = DateUtil.getMonth(current) == DateUtil.getMonth(old);
		}else {
			return result;
		}

		if(result){
			result = DateUtil.getDay(current) == DateUtil.getDay(old);
		}

		return result;

	}

	/**
	 * �ж���������Ĳ�ֵ���������ڰ���
	 * @param current
	 * @param old
	 * @return -1 ��ʾ������
	 */

	public static int getDateDurationDays(long current,long old) {

		final int DAY = 1440;  // һ��ķ���ֵ
		final int DAYS3 = 1440 * 2;  // һ��ķ���ֵ

		int  duratrion = -1;

		long duration = (current - old) / (1000 * 60);

		int hour = getHour(current);
		int minute = getMinute(current);
		int second = getSecond(current);

		long currentDayPassMillens = (hour*3600 + minute*60 + second)*1000;
		long currendDaymillensBegin = current - currentDayPassMillens;
		long yesterdaymillensBegin = current - currentDayPassMillens - DAY*60*1000;
		long beforeyesterdaymillensBegin = yesterdaymillensBegin - DAY*60*1000;

		if (duration < DAYS3 && duration >= 0 ) {
			if(old >= currendDaymillensBegin){
				duratrion = 0;
			}else if(old<currendDaymillensBegin && old >= yesterdaymillensBegin) {
				duratrion = 1;
			}else if(old >=beforeyesterdaymillensBegin &&  old < yesterdaymillensBegin) {
				duratrion = 2;
			}
		} else {
			duratrion = (int) (duration / DAY);
		}

		return duratrion;

	}




	public static int getYear(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.SECOND);
	}

	public static String getTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
		return format.format(new Date(time));
	}

	public static String getDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd");
		return format.format(new Date(time));
	}

	public static String getReplyDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
		return format.format(new Date(time));
	}

	public static String formatDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date(time));
	}
	public static String getDetailDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return format.format(new Date(time));
	}

	public static String getApprovalDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\n     HH:mm");
		return format.format(new Date(time));
	}

	public static String getHourAndMin(long time) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		return format.format(new Date(time));
	}

	public static String getEventDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
		return format.format(new Date(time));
	}

	public static String getYearDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		return format.format(new Date(time));
	}

    public static String getAppMandD(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM.dd");
        return format.format(new Date(time));
    }

    public static String getAppYandMandD(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        return format.format(new Date(time));
    }

	public static String getYearDate1(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd E");
		return format.format(new Date(time));
	}

	public static String getSubClassDate(long time) {
		SimpleDateFormat format = new SimpleDateFormat("E\nMM月dd号");
		return format.format(new Date(time));
	}
	public static String getSubDateTitle(long time) {
		SimpleDateFormat format = new SimpleDateFormat("MM月dd日 E");
		return format.format(new Date(time));
	}

	public static String getSubDateTitle2(long time) {
		SimpleDateFormat format = new SimpleDateFormat("E MM月dd日");
		return format.format(new Date(time));
	}

	public static String getWeek(long time) {
		SimpleDateFormat format = new SimpleDateFormat("E");
		return format.format(new Date(time));
	}

	public static String getAllTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		return format.format(new Date(time));
	}

	public static String getApprovalTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 (E)");
		return format.format(new Date(time));
	}

	public static String getScheduleTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy_M_d");
		return format.format(new Date(time));
	}

	public static String getMDHMTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
		return format.format(new Date(time));
	}
	public static String getChatTime(long timesamp) {
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		Date today = new Date(System.currentTimeMillis());
		Date otherDay = new Date(timesamp);
		int temp = Integer.parseInt(sdf.format(today))
				- Integer.parseInt(sdf.format(otherDay));
		switch (temp) {
			case 0:
				result = "今天 "+getHourAndMin(timesamp);
				break;
			case 1:
				result = "昨天 " + getHourAndMin(timesamp );
				break;
			default:
				if(getYear(otherDay)==getYear(today)){
					result = getEventDate(timesamp);
				}else{
					result =getYearDate(timesamp);
				}
				break;
		}
		return result;
	}

	public static String getUpDateTime(long timesamp) {
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		Date today = new Date(System.currentTimeMillis());
		Date otherDay = new Date(timesamp);
		int temp = Integer.parseInt(sdf.format(today))
				- Integer.parseInt(sdf.format(otherDay));
		switch (temp) {
			case 0:
				result = "今天 " + getHourAndMin(timesamp);
				break;
			case 1:
				result = "昨天 " + getHourAndMin(timesamp );
				break;
			default:
				if(getYear(otherDay) == getYear(today)){
					result = getReplyDate(timesamp);
				}else{
					result =getDetailDate(timesamp);
				}
				break;
		}
		return result;
	}

	public static long jsonTime2Mills(String jTime) {
		// "update_time":"2012-11-28T02:00:36"
		try {
			return FORMAT.parse(jTime.replace('T', ' ')).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取当天的日期时间戳
	 * @return
	 */
	public static long getDayBegin() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 001);
		return cal.getTimeInMillis()/1000;
	}

	/**
	 * 获取当月第一天的日期时间戳
	 * @return
	 */
	public static long getCurrentMonthBegin() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 001);
		return cal.getTimeInMillis()/1000;
	}

	/**
	 * 获取某年某月第一天的日期时间戳
	 * @return
	 */
	public static long getMonthBegin(int year , int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 001);
		return cal.getTimeInMillis()/1000;
	}

	/**
	 * 获取某年某月某天的日期时间戳
	 * @return
	 */
	public static long getOneDayBegin(int year , int month ,int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 001);
		return cal.getTimeInMillis()/1000;
	}


}
